class Shop::SpuDetailsController < ShopController
  before_action :set_spu

  def index
    @spu_details = @spu.spu_details.order("sort ASC")
  end

  def create
    @spu_detail = @spu.spu_details.build spu_detail_params
    if @spu_detail.save
      redirect_to shop_spu_spu_details_path(@spu), notice: "保存成功！"
    else
      redirect_to shop_spu_spu_details_path(@spu), notice: "保存失败！"
    end
  end
  
  def destroy
    @spu_detail = SpuDetail.find params[:id]
    if @spu_detail.destroy
      redirect_to shop_spu_spu_details_path(@spu), notice: "删除成功."
    else
      redirect_to shop_spu_spu_details_path(@spu), notice: "删除失败."
    end
  end

  private

  def set_spu
    @spu = Spu.find params[:spu_id]
  end

  def spu_detail_params
    params.require(:spu_detail).permit(:image, :sort, :spu_id)
  end
end
